package com.xr.oa.meeting;

import com.bstek.dorado.common.*;
import com.bstek.dorado.data.*;
import com.bstek.dorado.data.db.DBStatement;
import com.bstek.dorado.view.*;
import com.bstek.dorado.view.data.*;
import com.bstek.dorado.view.control.*;
import com.xr.common.CommUtil;
import com.xr.common.DBTools;
import com.xr.common.mail.Mail;

/**
 * selectEmployeeViewModel
 */
public class selectEmployeeViewModel extends DefaultViewModel {
  protected void doUpdateData(ParameterSet parameters, ParameterSet outParameters)
      throws Exception {
	  DBStatement dbs=new DBStatement();
	    Dataset dsEmp=getDataset("datasetQueryEmployee");
	    Dataset dsM=getDataset("datasetMail");
	    boolean mailSucc=true;
	    int ifMail;
	    String toMail="";
	    dsEmp.moveFirst();
	    while(!dsEmp.isLast()){
	    	if(dsEmp.getString("MAIL")!=null && dsEmp.getString("MAIL").indexOf("@")>0)
	    	toMail+=dsEmp.getString("MAIL")+",";
	    	dsEmp.moveNext();
	    }
	    
	    try{
	    	sendEmail(dsM,toMail.substring(0,toMail.length()-1));
	    }catch(Exception e){
	    	mailSucc=false;
	    }
	    
	    dsEmp.moveFirst();
	    while(!dsEmp.isLast()){
	    	if(mailSucc)ifMail=dsEmp.getInt("IF_MAIL");else ifMail=0;
	    	 dbs.setSql("insert into oa_meeting_party(meeting_party_id, meeting_time_id, employee_id, if_mail)values(seq_oa.nextval,"+dsM.getString("MEETING_TIME_ID")+" ,"+dsEmp.getString("EMPLOYEE_ID")+", "+ifMail+")");
	    	 dbs.execute();
	    	dsEmp.moveNext();
	    }
	    dbs.setSql("update OA_MEETING_TIME set MAIL_SUBJECT=:MAIL_SUBJECT,MAIL_CONTEXT=:MAIL_CONTEXT where MEETING_TIME_ID=:MEETING_TIME_ID");
	    dbs.parameters().setString("MAIL_SUBJECT", dsM.getString("MAIL_SUBJECT"));
	    dbs.parameters().setString("MAIL_CONTEXT", dsM.getString("MAIL_CONTEXT"));
	    dbs.parameters().setString("MEETING_TIME_ID", dsM.getString("MEETING_TIME_ID"));
	    dbs.execute();
	    
	    dbs.close();
  }

  private void sendEmail(Dataset ds,String to) throws Exception {
 
		try { 
			//UtilTools.getUserConfig("welcomePageTitle");
			  String addr=DBTools.getUserConfig("open_account_mail_from");
			  String host=DBTools.getUserConfig("open_account_smtp"); 
			String subject = ds.getString("SUBJECT");
			String content = ds.getString("CONTEXT");
			
			Mail mail = new Mail(ds.getString("MAIL"), ds.getString("MAIL_SMTP"), ds.getString("MAIL"), ds.getString("MAIL_PWD"));
			mail.setTo(to);
			//mail.setCc(addr);
			mail.setSubject(subject);
			mail.setContent(content);
			mail.sendMail();
		} finally {
			 
		}

	}

}
